﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using System.Web;
using Skynet.BusinessLogic.BusinessComponents;

namespace Skynet.WebAdministracion.Views.ExamRunningMgm
{
    [System.ComponentModel.DataObject]
    public class ExamRunningMgmPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IExamRunningMgmView>
    {
        [Dependency]
        public WebFoundation.Services.Session.ISessionService SessionService { get; set; }


        [Dependency]
        public WebFoundation.Services.Navigation.INavigationService NavigationService { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }


        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }
        [Dependency]
        public IBCCustomersQueries BCCustomersQueries { get; set; }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public object GetRunningExams(Guid ExamByCustomerId)
        {
            if (ExamByCustomerId == Guid.Empty)
            {
                return null;
            }
            var exam = this.BCExamQueries.SearchExamByCustomer(options: BusinessLogic.BusinessComponents.ExamByCustomerSearchOptions.RuntimeByCustomerExamId, ExamByCustomerId: ExamByCustomerId).SingleOrDefault();
            var query = new { Id = exam.Id, Category = exam.Exam.ExamCategory.Title, Title = exam.Exam.Title, Duration = exam.Duration, ValidFrom = exam.ValidFrom, ValidTo = exam.ValidTo };
            return query;
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public IEnumerable<object> GetRunningExams()
        {
            var userName = HttpContext.Current.User.Identity.Name;

            HttpContext.Current.Items["Culture"] = "es"; 
            

            var customer = this.BCCustomersQueries.FindCustomers(option: BusinessLogic.BusinessComponents.CustomerFindOptions.GetByUserName, UserName: userName).SingleOrDefault();
            var exams = this.BCExamQueries.SearchExamByCustomer(options: ExamByCustomerSearchOptions.RuntimeRunningByCustomerId, CustomerId: customer.Id);
            var query = (from c in exams select new { Id = c.Id, Category = c.Exam.ExamCategory.Title, Title = c.Exam.Title, Duration = c.Duration }).ToList();
            return query;
        }

        public void ReAcceptExam(Guid CustomerExamId)
        {

            

             this.SessionService.RuntimeExamByCustomerId = CustomerExamId;
            this.SessionService.RuntimeExamQuestions = new LinkedList<BusinessEntities.BaseQuestion>(this.BCExamQueries.SearchQuestion(QuestionSearchOptions.RuntimeByCustomerExamId, CustomerExamId:CustomerExamId));
            this.SessionService.RuntimeExamQuestionPosition = 0;
            this.NavigationService.Navigate("~/Evaluation/ExamReviewViewer.aspx");

        }


    }
}
